package com.example.springbootServiceNetwork.demos.web.Service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootServiceNetwork.demos.web.Config.JwtInfo;
import com.example.springbootServiceNetwork.demos.web.Config.Result;
import com.example.springbootServiceNetwork.demos.web.Config.ResultEnum;
import com.example.springbootServiceNetwork.demos.web.Dao.UserDao;
import com.example.springbootServiceNetwork.demos.web.Dao.UserPurviewDao;
import com.example.springbootServiceNetwork.demos.web.Dto.PageDto;
import com.example.springbootServiceNetwork.demos.web.Entity.User;
import com.example.springbootServiceNetwork.demos.web.Entity.UserPurview;
import com.example.springbootServiceNetwork.demos.web.Service.UserPurviewService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static com.example.springbootServiceNetwork.demos.web.Tool.WrapperMethodUtil.bollPurvieTool;

@Service
public class UserPurviewServiceImpl implements UserPurviewService {
    @Resource
    private UserDao userDao;
    @Resource
    private UserPurviewDao userPurviewDao;

    /**
     * 查找用户权限
     */
     @Override
     public Result  qureyUserPurview(PageDto pageDto, JwtInfo jwtInfo){
         Result result = new Result();
         try {
             if (bollPurvieTool(jwtInfo.getPurview(),"superAdmin" )){
                 //创建分页
                 Page<User> page = new Page<>(pageDto.getPage(),pageDto.getLimit());
                 //获取到分页集合
                 Page<User> userPage = userDao.selectPage(page, null);
                 List<User> userList = userPage.getRecords();
                 if (!userList.isEmpty()){
                     List<UserPurview> purviewList = new ArrayList<>();
                     for (User item : userList){
                         UserPurview  userPurview =JSON.parseObject(JSON.toJSONString(item),UserPurview.class);
                         UserPurview  purview= userPurviewDao.selectById(item.getUserId());
                         userPurview.setUserPurview(purview.getUserPurview());
                         purviewList.add(userPurview);
                     }
                     // 返回对象得到分页所有数据
                     result.setCount(Math.toIntExact(userDao.selectCount(null)));
                     result.setData(purviewList);
                     result.setMsg("用户权限列表查询成功");
                     result.setCode(ResultEnum.SUCCESS.getCode());
                 }else {
                     result.setCode(ResultEnum.SUCCESS.getCode());
                     result.setMsg("用户权限列表为空");
                 }
             }else {
                 result.setCode(ResultEnum.SUCCESS.getCode());
                 result.setMsg("您暂无权限！");
             }
         }catch (Exception e){
             result.setCode(ResultEnum.UNKNOWNERROR.getCode());
             result.setMsg(ResultEnum.DB_QUERY_FAILURE.getData());
             result.setData(e.getCause());
         }
         return result;
     }

     /**
     * 修改用户权限
     */
     @Override
     public Result  updateUserPurview(UserPurview userPurview, JwtInfo jwtInfo){
         Result result = new Result();
         try {
             if (bollPurvieTool(jwtInfo.getPurview(),"superAdmin" )){
                 int res = userPurviewDao.updateById(userPurview);
                 if (res==1){
                     result.setMsg("权限修改成功");
                     result.setCode(ResultEnum.SUCCESS.getCode());
                 }else {
                     result.setCode(ResultEnum.SUCCESS.getCode());
                     result.setMsg("权限修改失败！");
                 }
             }else {
                 result.setCode(ResultEnum.SUCCESS.getCode());
                 result.setMsg("您暂无权限！");
             }
         }catch (Exception e){
             result.setCode(ResultEnum.UNKNOWNERROR.getCode());
             result.setMsg(ResultEnum.DB_QUERY_FAILURE.getData());
             result.setData(e.getCause());
         }
         return result;
     }

}
